﻿using System;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.News;
using ChannelVN.Cached.UrlRewrite;
using GUI.Extensions;

namespace GUI.Pages.News
{
    public partial class ListNewsByDate : System.Web.UI.Page
    {

        private const int PageSize = 20;
        private const int TopOtherNews = 10;

        protected void Page_Load(object sender, EventArgs e)
        {
            var objCategory = RequestExtensions.Category;
            if (objCategory != null)
            {
                #region LIST NEWS BY DATE

                var day = RequestExtensions.Day;
                var month = RequestExtensions.Month;
                var year = RequestExtensions.Year;

                var text = string.Empty;
                text += day > 0 ? " ngày " + day : string.Empty;
                text += month > 0 ? " tháng " + month : string.Empty;
                text += year > 0 ? " năm " + year : string.Empty;

                ltrTitle.Text = string.Format("Tin {0} ", objCategory.Name.ToLower()) + text;

                var containsPageIndex = RequestExtensions.ContainsPageIndex;
                var pageIndex = RequestExtensions.PageIndex;
                string pageTitle = string.Format(
                "Xem tin theo ngày | {0} | soha.vn {1}", text, containsPageIndex ? string.Empty : "| Page " + pageIndex
                );


                var dt = NewsBO.GetInstance().GetNewsByDate(day, month, year, pageIndex, PageSize, objCategory.Id);
                ctrListNews.Visible = ctrListNews.DataBind(dt);
                if (dt != null && dt.Rows.Count > 0)
                {
                    ctrPaging.SumPage = NewsBO.GetInstance().GetTotalPagesOfListNewsByDate(day, month, year, PageSize, objCategory.Id);
                    ctrPaging.LinkSite = RequestExtensions.Url;
                    ctrPaging.Padding(pageIndex);
                }
                PresentationUtils.AddBasicMetaData(Page, pageTitle, pageTitle);

                #endregion

                {
                    if (objCategory.Id == DefinedConstants.Zone.TheThao || objCategory.ParentId == DefinedConstants.Zone.TheThao)
                    {
                        phSubCategories.Controls.Add(LoadControl("/Controls/Sport/SubCategories.ascx"));
                    }
                    else
                    {
                        phSubCategories.Controls.Add(LoadControl("/Controls/News/SubCategories.ascx"));
                    }
                }

                #region OtherNews
                {
                    divOtherNews.Visible = ctrOtherNews.DataBind(NewsBO.GetInstance().GetNewsByPosition(top: TopOtherNews, typeId: DefinedConstants.News.TypeIDs.TinTrangChuyenMuc, zoneId: objCategory.Id));
                }
                #endregion

                var mtaCategory = NewsBO.GetInstance().GetCategoryMetaData(objCategory.Id);
                var mtaParentCategory = objCategory.ParentId > 0
                                            ? NewsBO.GetInstance().GetCategoryMetaData(objCategory.ParentId)
                                            : mtaCategory;
                #region Css/Js
                {
                    if (mtaParentCategory != null && !string.IsNullOrEmpty(mtaParentCategory.ThemeUrl))
                    {
                        PresentationUtils.AddCss(Page, mtaParentCategory.ThemeUrl, true);
                    }
                }
                #endregion

                return;
            }

            _301Redirection.RedirectToFileNotFoundPage();
        }
    }
}